package com.lagou.controller;

import com.lagou.domain.*;
import com.lagou.service.MenuService;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 根据条件查询所有角色
     */
    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role){

        List<Role> allRole = roleService.findAllRole(role);

        ResponseResult responseResult = new ResponseResult(true, 200, "查询所有角色信息成功", allRole);

        return responseResult;
    }

    /**
     * 按照条件查询所有的父子菜单信息 (分配菜单的第一个接口)
     */

    @Autowired
    private MenuService menuService;

    @RequestMapping("/findAllMenu")
    public ResponseResult findSubMenuListByPid(){

        //-1表示查询所有的父子级菜单
        List<Menu> menuList = menuService.findSubMenuListByPid(-1);

        //响应数据
        //根据接口文档可知需要设置key值为parentMenuList
        HashMap<String, Object> map = new HashMap<>();
        map.put("parentMenuList",menuList);

        ResponseResult responseResult = new ResponseResult(true, 200, "查询父子级菜单成功", map);

        return responseResult;
    }

    /**
     * 添加&修改角色
     */
    @RequestMapping("/saveOrUpdateRole")
    public ResponseResult saveOrUpdateRole(@RequestBody Role role){

        if (role.getId() == null) {

            //新增
            roleService.saveRole(role);

            ResponseResult responseResult = new ResponseResult(true, 200, "添加角色成功", null);

            return responseResult;
        }else {

            //更新
            roleService.updateRole(role);

            ResponseResult responseResult = new ResponseResult(true, 200, "更新角色成功", null);

            return responseResult;
        }
    }


    /**
     * 根据角色id查询该角色关联的菜单信息id[1,2,3,4]
     */
    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(Integer roleId){

        List<Integer> menuByRoleId = roleService.findMenuByRoleId(roleId);

        ResponseResult responseResult = new ResponseResult(true, 200, "查询角色关联的菜单信息成功", menuByRoleId);

        return responseResult;
    }

    /**
     * 根据id回显角色信息
     */
    @RequestMapping("/findRoleById")
    public ResponseResult findRoleById(Integer id){

        Role roleById = roleService.findRoleById(id);

        ResponseResult responseResult = new ResponseResult(true, 200, "根据id查询角色成功", roleById);

        return responseResult;
    }

    /**
     * 为角色分配菜单
     */
    @RequestMapping("/roleContextMenu")
    public ResponseResult roleContextMenu(@RequestBody RoleMenuVo roleMenuVo){

        roleService.roleContextMenu(roleMenuVo);

        ResponseResult responseResult = new ResponseResult(true, 200, "为角色分配菜单成功", null);

        return responseResult;
    }

    /**
     * 删除角色
     */
    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(Integer id){

        roleService.deleteRole(id);

        ResponseResult responseResult = new ResponseResult(true, 200, "删除角色成功", null);

        return responseResult;
    }

    /**
     * 根据角色id获取该拥有的资源信息
     */
    @RequestMapping("/findResourceListByRoleId")
    public ResponseResult findResourceListByRoleId(Integer roleId){


        List< ResourceCategory> resourceCategoryByRoleId = roleService.findResourceCategoryByRoleId(roleId);
        List<Resource> resourceListByRoleId = roleService.findResourceListByRoleId(roleId);

        HashMap<String, Object> map = new HashMap<>();
        map.put("content",resourceCategoryByRoleId);
        map.put("resourceList",resourceListByRoleId);

        ResponseResult responseResult = new ResponseResult(true, 200, "获取当前角色拥有的资源信息成功", map);

        return responseResult;
    }

    /**
     * 分配资源
     */
    @RequestMapping("/roleContextResource")
    public ResponseResult roleContextResource(@RequestBody RoleResourceVo roleResourceVo){

        roleService.roleContextResource(roleResourceVo);

        return new ResponseResult(true,200,"分配资源成功",null);

    }

}
